package com.jason.system.service.impl;

import com.jason.system.entity.AdminPermission;
import com.jason.system.mapper.PermissionMapper;
import com.jason.system.service.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/4/1 21:30
 */

@Service
public class PermissionServiceImpl implements PermissionService {

  @Resource
  PermissionMapper permissionMapper;

  @Override
  public List<AdminPermission> getAll() {
    return permissionMapper.getAllPermissions();
  }

  @Override
  public boolean insertToRolePermission(Integer rId, String pIds) {
    if("".equals(pIds)||rId == null){
      return false;
    }
    List<Integer> pIdList = Arrays.stream(pIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
    for (Integer pId : pIdList){
      permissionMapper.insertToRolePermission(rId,pId);
    }
    return true;
  }

  @Override
  public boolean deleteToRolePermission(Integer rId, String pIds) {
    if("".equals(pIds)||rId == null){
      return false;
    }
    List<Integer> pIdList = Arrays.stream(pIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
    for (Integer pId : pIdList){
      permissionMapper.deleteToRolePermission(rId,pId);
    }
    return true;
  }
}
